package cn.hp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import cn.hp.domain.PageBean;
import cn.hp.domain.Product;
import cn.hp.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	
	
	//分页助手全查所有的信息
	@RequestMapping("/findAll")
	public ModelAndView findAll(@RequestParam(defaultValue="1") Integer currPage,@RequestParam(defaultValue="5")Integer pageSize){
		PageInfo<Product> pageInfo = productService.findByPageHelper(currPage, pageSize);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("pageInfo", pageInfo);
		modelAndView.setViewName("product-list");
		return modelAndView;
		
	}
	
	
	
	
	
	//手动分页全查
	@RequestMapping("/findAll2")
	public ModelAndView findAll2(@RequestParam(defaultValue="1") Integer currPage,@RequestParam(defaultValue="5")Integer pageSize){
		PageBean<Product> pageBean=productService.findByPage(currPage,pageSize);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("pageBean", pageBean);
		modelAndView.setViewName("product-list");
		return modelAndView;
		
	}
	
	
	
	
	
	//查询所有商品信息
	@RequestMapping("/findAll1")
	public ModelAndView findAll1(){
		List<Product> productList=productService.findAll();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("productList", productList);
		modelAndView.setViewName("product-list");
		return modelAndView;
	}
	
	
	
	//查询所有商品信息
	@RequestMapping("/save")
	public String save(Product product){
		productService.save(product);
		return "redirect:findAll";
	}
	
	//根据ID查询产品,数据回显
	@RequestMapping("/updateUI")
	public ModelAndView updateUI(Integer id){
		Product product= productService.findById(id);
		ModelAndView andView=new ModelAndView();
		andView.addObject("product", product);
		andView.setViewName("product-update");
		return andView;
	}
		
	//修改产品信息
	@RequestMapping("/update")
	public String update(Product product){
		productService.update(product);
		return "redirect:findAll";
	}
	
	
	
	
	//删除单个
	@RequestMapping("/delOne")
	public String delOne(Integer id){
		productService.delOne(id);
		return "redirect:findAll";
		
	}
	
	
	//批量删除
	@RequestMapping("/delMany")
	public String delMany(Integer[] ids){
		productService.delMany(ids);
		return "redirect:findAll";
		
	}
	
	
}
